package workforce.limousine.struts;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.util.SelectItem;
import limousine.vo.Users;
import limousine.vo.Users;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;

public class UsersManagerInit extends Action {

    private String order = "";
    private Func tl = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***UsersManagerInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if(!user.getRole_name().equals("Admin")){
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        session.removeAttribute(Token.USERS);
        session.removeAttribute(Token.USERS_ROLE_LIST);
        session.removeAttribute(Token.USERS_GENDER_LIST);
        session.removeAttribute(Token.USERS_ENABLE_LIST);
        session.removeAttribute(Token.USERS_STATUS_LIST);
        session.removeAttribute(Token.USERS_PAGE);
        session.removeAttribute(Token.USERS_CURRENT_OFFSET);


        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        DynaActionForm f = (DynaActionForm) form;
        f.set("userName", "");
        f.set("name", "");
        //f.set("password", "");
        //f.set("phone", "");
        f.set("email", "");


        order = request.getParameter("order");
        order = "order by " + ((order == null) ? "user_name" : order);

/*
        List<Users> l = getUser(ds,user.getRoleId());
        System.out.println("size " + l.size());
        if (l.size() > 0) {
            session.setAttribute(Token.USERS, l);
        }
*/
        
        List<SelectItem> roleList = service.getRoleListByPrivilege(ds,user.getRoleId(),"",true);
        if (roleList.size() > 0) {
            session.setAttribute(Token.USERS_ROLE_LIST, roleList);
        }

        List<SelectItem> enableList = service.getEnableList("", true);
        if (enableList.size() > 0) {
            session.setAttribute(Token.USERS_ENABLE_LIST, enableList);
        }

        List<SelectItem> statusList = service.getStatusList("", true);
        if (statusList.size() > 0) {
            session.setAttribute(Token.USERS_STATUS_LIST, statusList);
        }

        List<SelectItem> genderList = service.getGenderList("", true);
        if (genderList.size() > 0) {
            session.setAttribute(Token.USERS_GENDER_LIST, genderList);
        }


        service.updateControlPanel(session, ds,user.getRoleId());//update control panel display

        return mapping.findForward(Token.SUCCESS);
    }

    public List<Users> getUser(DataSource ds,String userRoleId) {
        StringBuffer sql = new StringBuffer();
        sql.append("Select *,(select name from role where id=role_id ) as ROLE_NAME From users where role_id > '"+userRoleId+"' order by user_name");
        List<Users> l = new ArrayList<Users>();
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            while (rs.next()) {
                Users o = new Users();
                o.setId(rs.getString("ID"));
                o.setUserName(rs.getString("USER_NAME"));
                o.setPassword(rs.getString("PASSWORD"));
                o.setName(rs.getString("NAME"));
                o.setEmail(rs.getString("EMAIL"));
                o.setPhone(rs.getString("PHONE"));
                o.setGender(rs.getString("GENDER"));
                o.setCreateDate(rs.getString("CREATE_DATE"));
                o.setLastLogin(rs.getString("LAST_LOGIN"));
                o.setEnable(rs.getString("ENABLE"));
                o.setRoleId(rs.getString("ROLE_ID"));
                o.setStatus(rs.getString("STATUS"));
                o.setRoleName(rs.getString("ROLE_NAME"));
                l.add(o);
            }
            rs.close();
            stmt.close();
            conn.close();
            return l;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
